package ex.com.jm.food.action;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.MenuProfitSummary;
import ex.com.jm.food.service.AdminMPSService;
import ex.com.jm.food.utils.AccountDate;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.ExcelUtil;
import ex.com.jm.food.utils.JsonUtils;

public class AdminMPSAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private MenuProfitSummary mps;
	private AdminMPSService adminMPSService;
	private String menuName;
	private String order;
	private String sort;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private Map<String, String> dateMap;
	public MenuProfitSummary getMps() {
		return mps;
	}
	public void setMps(MenuProfitSummary mps) {
		this.mps = mps;
	}
	
	public AdminMPSService getAdminMPSService() {
		return adminMPSService;
	}
	public void setAdminMPSService(AdminMPSService adminMPSService) {
		this.adminMPSService = adminMPSService;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String mpsList(){
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		dateMap=DateUtils.getDate();
		return "mpsList";
	}
	
	public void getMPSs(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<MenuProfitSummary> mps=this.getAdminMPSService().getMPSs(offset, pageRow, menuName, s_date, e_date, sort, order);
		JSONArray jsonmps=JSONArray.fromObject(mps);
		totalCount=this.getAdminMPSService().mpsCount(menuName, s_date, e_date);
		 JSONObject jo=JsonUtils.buildJson(jsonmps, initPage, pageRow, totalCount);
			PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jo);
			out.flush();
			out.close(); 
	}
	
	public void MPSExcel(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("application/vnd.ms-excel");
		OutputStream out=null;
		try {
			out=response.getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<MenuProfitSummary> mps=this.getAdminMPSService().MPSExcel(menuName, s_date, e_date);
		String[] headers=new String[]{"菜单名称","所属企业","企业收益","饿9定收益","售出总量","收益率"};
		String[] columns=new String[]{"menuName","comName","comTotalProfit","totalProfit","totalNum","profitRate"};
		try {
			ExcelUtil.MPSExcel(mps, headers, columns, out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void MPSReport(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<MenuProfitSummary> mpss=this.getAdminMPSService().MPSReport(menuName, s_date, e_date);
		
		List<String> dateList=AccountDate.getEveryday(s_date, e_date);
		List<Double> totalProfit=new ArrayList<Double>();
		List<Double> comTotalProfit=new ArrayList<Double>();
		List<Integer> totalNum=new ArrayList<Integer>();
		List<Integer> profitRate=new ArrayList<Integer>();
		for (MenuProfitSummary mps : mpss) {
			totalProfit.add(mps.getTotalProfit());
			comTotalProfit.add(mps.getComTotalProfit());
			totalNum.add(mps.getTotalNum());
			profitRate.add(mps.getProfitRate());
		}
		JSONArray jototalprofit=JSONArray.fromObject(totalProfit);
		JSONArray jocomTotalProfit=JSONArray.fromObject(jototalprofit);
		JSONArray jototalNum=JSONArray.fromObject(totalNum);
		JSONArray joprofitRate=JSONArray.fromObject(profitRate);
		JSONArray jodate=JSONArray.fromObject(dateList);
		JSONObject jo=new JSONObject();
		jo.put("totalProfit", jototalprofit);
		jo.put("comTotalProfit", jocomTotalProfit);
		jo.put("totalNum", jototalNum);
		jo.put("profitRate", joprofitRate);
		jo.put("date", jodate);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
}
